package com.hqz.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqz.entity.Users;
import com.hqz.entity.UsersRole;
import com.hqz.mapper.UsersMapper;
import com.hqz.mapper.UsersRoleMapper;
import com.hqz.service.UsersService;
import com.hqz.utils.ShiroUtils;
import com.hqz.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private UsersRoleMapper usersRoleMapper;

    @Override
    public IPage<Users> selectList(Page<Users> page) {
        return usersMapper.selectList(page);
    }

    @Override
    public Users selectByName(String usersName) {
        return usersMapper.selectByName(usersName);
    }

    @Override
    public Integer add(Users users, Integer roleId) {
        //从ShiroUtiles类中随机生成盐
        users.setSalt(ShiroUtils.randomSalt());
        //将密码设置为加密后的密码（由ShiroUtile里面的encryptPassword方法实现）
        users.setPwd(ShiroUtils.encryptPassword(users.getPwd(),users.getCredentialsSalt()));
        users.setCreateTime(StringUtils.getNowTime());
        users.setUpdateTime(StringUtils.getNowTime());
        users.setIsDel(0);
        int result = usersMapper.insert(users);

        //将usersId和roleId一同插入到员工角色关系表
        UsersRole usersRole = new UsersRole();
        usersRole.setUsersId(users.getUsersId());
        usersRole.setRoleId(roleId) ;
        usersRole.setCreateTime(StringUtils.getNowTime());
        usersRole.setUpdateTime(StringUtils.getNowTime());
        usersRole.setIsDel(0);
        usersRoleMapper.insert(usersRole);
        return result;
    }

    @Override
    public Integer update(Users users,Integer roleId) {
        //判断用户是否输入密码，如果没有，获取的就是空字符串（“”），就不修改密码
        if(!"".equals(users.getPwd())){
            //从shiroUtile类中随机生成盐
            users.setSalt(ShiroUtils.encryptPassword(users.getPwd(),users.getCredentialsSalt()));
        }else {
            users.setPwd(null);
        }

        //设置更新时间
        users.setUpdateTime(StringUtils.getNowTime());
        //将信息更新到数据库中（空的数据不修改）
        int result = usersMapper.updateById(users);

        UsersRole usersRole = new UsersRole();
        usersRole.setUsersId(users.getUsersId());
        usersRole.setRoleId(roleId);
        usersRole.setUpdateTime(StringUtils.getNowTime());
        AbstractWrapper abstractWrapper = new QueryWrapper();
        abstractWrapper.eq("users_id",users.getUsersId());
        usersRoleMapper.update(usersRole,abstractWrapper);

        return result;
    }
}
